#include "workerManager.h"
#include "employee.h"
#include "manager.h"
#include "boss.h"
#include "worker.h"
#include <cstdlib>
#include <fstream>

WorkerManger::WorkerManger()
{
    //file not exist
    ifstream ifs;
    ifs.open(FILENAME, ios::in);
    if(!ifs.is_open())
    {
        cout << "文件不存在!" << endl;
        m_EmpCount = 0;
        m_EmpArray = nullptr;
        m_FileIsEmpty = true;
        ifs.close();
        return;
    }

    //file is existed, has record
    char ch;
    ifs >> ch;
    if(ifs.eof())
    {
        //文件为空
        cout << "文件不存在!" << endl;
        m_EmpCount = 0;
        m_EmpArray = nullptr;
        m_FileIsEmpty = true;
        ifs.close();
        return; 
    }
    
    //文件存在,并且存在记录
    int count = getEmpCount();
    cout << "职工人数为: " << count << endl;
    m_EmpCount = count;

    //初始化员工数据
    m_EmpArray = new Worker*[m_EmpCount];
    initEmp();
    for(int i = 0; i < m_EmpCount; i++)
    {
        cout << "职工编号: " << m_EmpArray[i]->m_Id
             << "姓名: " << m_EmpArray[i]->m_Name
             << "部门编号: " << m_EmpArray[i]->m_DeptId
             << endl;
    }
}

//显示职工
void WorkerManger::showEmp()
{
    if(m_FileIsEmpty)
    {
        cout << "文件不存在或记录为空!" << endl;
    }
    else 
    {
        for(int i = 0; i < m_EmpCount; i++)
        {
            m_EmpArray[i]->showInfo();
        }
    }

    system("pause");
    system("cls");
}

//显示菜单
void WorkerManger::showMenu()
{
    cout << "*****  欢迎使用职工管理系统  *****" << endl;
    cout << "*****  0.退出管理系统  *****" << endl;
    cout << "*****  1.增加职工信息  *****" << endl;
    cout << "*****  2.显示职工信息  *****" << endl;
    cout << "*****  3.删除职工信息  *****" << endl;
    cout << "*****  4.修改职工信息  *****" << endl;
    cout << "*****  5.查找职工信息  *****" << endl;
    cout << "*****  6.按照编号排序  *****" << endl;
    cout << "*****  7.清空所有文档  *****" << endl;
    cout << endl;
}

//退出系统
void WorkerManger::exitSystem()
{
    cout << "欢迎使用:" << endl;
    system("pause");
    exit(0);
}

//初始化员工
void WorkerManger::initEmp()
{
    ifstream ifs(FILENAME, ios::in);

    int id;
    string name;
    int dId;

    int index = 0;
    while(ifs >> id && ifs >> name && ifs >> dId)
    {
        Worker *worker = nullptr;

        //根据部门ID,创建不同对象
        if(dId == 1)
        {
            worker = new Employee(id, name, dId);
        }
        else if(dId == 2)
        {
            worker = new Manager(id, name, dId);
        }
        else if(dId == 3)
        {
            worker = new Boss(id, name, dId);
        }

        // 读取数据存入数组
        m_EmpArray[index] = worker;
        index++;
    }
    ifs.close();
}

//添加职工
void WorkerManger::AddEmp()
{
    cout << "请输入添加职工数:" << endl;
    //保存要添加的职工数
    int addNum = 0;
    cin >> addNum;

    if(addNum > 0)
    {
        int newSize = m_EmpCount + addNum;

        Worker **newSpace = new Worker*[newSize];

        //数据复制到更新的空间
        if(m_EmpArray != nullptr)
        {
            for(int i = 0; i < m_EmpCount; i++)
            {
                newSpace[i] = m_EmpArray[i];
            }
        }

        //添加新数据
        for(int i = 0; i < addNum; i++)
        {
            int id;    //职工编号
            string name; //职工姓名
            int dSelect;  // 部门选择

            cout << "请输入第 " << i + 1 << " 个职工编号: " << endl;
            cin >> id;

            cout << "请输入第 " << i + 1 << " 个职工姓名: " << endl;
            cin >> name;

            cout << "请选择该职工岗位: " << endl;
            cout << "1.普通职工" << endl;
            cout << "2.经理" << endl;
            cout << "3.老板" << endl;
            cin >> dSelect;

            Worker *worker = nullptr;
            switch(dSelect)
            {
                case 1:
                    worker = new Employee(id, name, 1);
                    break;
                case 2:
                    worker = new Manager(id, name, 2);
                    break;
                case 3:
                    worker = new Boss(id, name, 3);
                    break;
                default:
                    break;
            }

            //将创建职工职责,保存到数组
            newSpace[m_EmpCount + i] = worker;
        }
        //释放原有空间
        delete[] m_EmpArray;

        //指向新空间
        m_EmpArray = newSpace;

        //更新职工人数
        m_EmpCount = newSize;

        m_FileIsEmpty = false;
        cout << "成功添加: " << addNum << " 名新职工." << endl;
        //save to file
        save();
    }
    else
    {
        cout << "添加职工数错误!" << endl;
    }

    system("pause");
    system("cls");
}

//save to file
void WorkerManger::save()
{
    ofstream ofs;
    ofs.open(FILENAME, ios::out);

    for(int i = 0; i < m_EmpCount; i++)
    {
        ofs << m_EmpArray[i]->m_Id << " "
            << m_EmpArray[i]->m_Name << " "
            << m_EmpArray[i]->m_DeptId << endl;
    }
    ofs.close();
}

//统计职工数
int WorkerManger::getEmpCount()
{
    ifstream ifs(FILENAME, ios::in);

    int id;
    string name;
    int dId;

    int count = 0;
    while(ifs >> id && ifs >> name && ifs >> dId)
    {
        //记录人数
        count++;
    }

    ifs.close();
    return count;
}

// 员工是否存在
int WorkerManger::IsExist(int id)
{
    int index = -1;

    for(int i = 0; i < m_EmpCount; i++)
    {
        if(m_EmpArray[i]->m_Id == id)
        {
            index = i;
            break;
        }
    }

    return index;
}

//删除职工
void WorkerManger::DelEmp()
{
    if(m_FileIsEmpty)
    {
        cout << "文件不存在或者记录为空!" << endl;
    }
    else
    {
        cout << "请输入要删除的职工编号: " << endl;
        int id = 0; 
        cin >> id;
        int index = IsExist(id);

        if(-1 != index)
        {
            for(int i = index; i < m_EmpCount - 1; i++)
            {
                m_EmpArray[i] = m_EmpArray[i + 1];
            }
            m_EmpCount--;
            save();
            cout << "删除职工成功!" << endl;
        }
        else
        {
            cout << "删除失败!未找到该编号的职工" << endl;
        }
    }

    system("pause");
    system("cls");
}

//修改职工信息
void WorkerManger::modifyEmp()
{
    if(m_FileIsEmpty)
    {
        cout << "文件不存在或者记录为空!" << endl;
    }
    else
    {
        cout << "请输入修改职工的编号: " << endl;
        int id; 
        cin >> id;

        int ret = IsExist(id);
        if(-1 != ret)
        {
            delete m_EmpArray[ret];

            int newId = 0;
            string name = "";
            int dSelect = 0;

            cout << "找到: " << id << "号职工,请输入新职工编号" << endl;
            cin >> newId;

            cout << "请输入新职工姓名: " << endl;
            cin >> name;

            cout << "请输入新职工部门" << endl;
            cout << "1.普通职工" << endl;
            cout << "2.经理" << endl;
            cout << "3.老板" << endl;

            Worker *worker = nullptr;
            switch(dSelect)
            {
                case 1:
                    worker = new Employee(newId, name, dSelect);
                    break;
                case 2:
                    worker = new Manager(newId, name, dSelect);
                    break;
                case 3:
                    worker = new Boss(newId, name, dSelect);
                    break;
                default:
                    break;
            }

            m_EmpArray[ret] = worker;

            cout << "修改信息成功!" << m_EmpArray[ret]->m_DeptId << endl;
            save();
        }
        else
        {
            cout << "修改失败!" << endl;
        }
    }

    system("pause");
    system("cls");
}


//查找职工
void WorkerManger::findEmp()
{
    if(m_FileIsEmpty)
    {
        cout << "文件不存在或记录为空!" << endl;
    }
    else
    {
        cout << "请输入查找方式: " << endl;
        cout << "1.按职工编号查找." << endl;
        cout << "2.按职工姓名查找." << endl;

        int select = 0;
        cin >> select;
        
        if(1 == select)
        {
            int id;
            cout << "请输入要查找的职工编号:" << endl;
            cin >> id;

            int ret = IsExist(id);
            if(-1 != ret)
            {
                cout << "查找职工编号成功! 该员工信息如下: " << endl;
                m_EmpArray[ret]->showInfo();
            }
            else
            {
                cout << "未查找到该员工的职工编号!" << endl;
            }
        }
        else if(2 == select)
        {
            string name;
            cout << "请输入要查找的员工姓名: " << endl;
            cin >> name;

            bool flag = false;
            for(int i = 0; i < m_EmpCount; i++)
            {
                if(m_EmpArray[i]->m_Name == name)
                {
                    cout << "查找职工姓名成功! 该员工编号为: " 
                         << m_EmpArray[i]->m_Id
                         << " 的信息如下:" << endl;

                    flag = true;     
                    m_EmpArray[i]->showInfo();
                }
            }
            if(flag == false)
            {
                //未查找到该姓名
                cout << "查找失败,未找到该员工姓名!" << endl;
            }
        }
        else
        {
            cout << "输入选项有误!" << endl;
        }
    }

    system("pause");
    system("cls");
}


//按职工编号排序
void WorkerManger::sortEmp()
{
    if(m_FileIsEmpty)
    {
        cout << "文件不存在或记录为空!" << endl;
        system("pause");
        system("cls");
    }
    else
    {
        cout << "请选择排序方式: " << endl;
        cout << "1.按职工编号升序." << endl;
        cout << "2.按职工编号降序." << endl;

        int select = 0;
        cin >> select;

        for(int i = 0; i < m_EmpCount; i++)
        {
            int minOrMax = i;
            for(int j = i + 1; j < m_EmpCount; j++)
            {
                if(1 == select)
                {   //升序
                    if(m_EmpArray[minOrMax]->m_Id > m_EmpArray[j]->m_Id)
                    {
                        minOrMax = j;
                    }
                }
                else if(2 == select)
                {   //降序
                    if(m_EmpArray[minOrMax]->m_Id < m_EmpArray[j]->m_Id)
                    {
                        minOrMax = j;
                    }
                }
            }

            if(i != minOrMax)
            {
                Worker *tmp = m_EmpArray[i];
                m_EmpArray[i] = m_EmpArray[minOrMax];
                m_EmpArray[minOrMax] = tmp;
            }
        }
        cout << "排序成功! 排序后的结果为: " << endl;
        save();
        showEmp();
    }
}

//清空文档
void WorkerManger::cleanFile()
{
    cout << "确认清空?" << endl;
    cout << "1,确认." << endl;
    cout << "2,返回." << endl;

    int select = 0;
    cin >> select;

    if(1 == select)
    {
        ofstream ofs(FILENAME, ios::trunc);
        ofs.close();

        if(m_EmpArray != nullptr)
        {
            for(int i = 0; i < m_EmpCount; i++)
            {
                if(m_EmpArray[i] != nullptr)
                {
                    delete m_EmpArray[i];
                }
            }

            m_EmpCount = 0;
            delete[] m_EmpArray;
            m_EmpArray = nullptr;
            m_FileIsEmpty = true;
        }
        cout << "清空成功!" << endl;
    }
    else if(2 == select)
    {
        system("cls");
        showMenu();
        system("pause");
    }
    else
    {
        cout << "输入错误！请重新输入!" << endl;
    }

    system("pause");
    system("cls");
}

WorkerManger::~WorkerManger()
{
    if(m_EmpArray != nullptr)
    {
        for(int i = 0; i < m_EmpCount; i++)
        {   
            if(m_EmpArray[i] != nullptr)
            {
                delete m_EmpArray[i];
            }
        }
        delete[] m_EmpArray;
        m_EmpArray = nullptr;
    }
}